package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.aibkAnalysisZHT

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperPerAnalysisInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.AnalysisInforIndexHelp.getChildListBean
import com.lancoo.znbkxx.teacher.widget.CustomTextViewToPicture
import kotlinx.android.synthetic.main.aibk_adapter_analysis_item_child_jdt.view.*
import kotlinx.android.synthetic.main.aibk_adapter_analysis_item_stuanswer.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_childask.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanalysis.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanswer.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemscore.view.*

class AnalysisAdapterChildJdt(
        val activity: AppCompatActivity,
        val paperPerAnalysisInfor: PaperPerAnalysisInfor,
        val index_A: Int,
        val index_B: Int,
        val index_C: Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        var rootView:View? = null
         if(position == 0){
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_childask, parent, false)
        }else {
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_analysis_item_child_jdt, parent, false)
        }
        return rootView
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        if(position == 0){
            var childAsk = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildAsk
            val genreID = paperPerAnalysisInfor.TypeList[index_A].QuesDetailList[index_B].GenreID
            val sortIndex = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ItemList[0].SortIndex
            val childAnswerType = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildAnswerType
            val typeNo = paperPerAnalysisInfor.TypeList[index_A].QuesDetailList[index_B].TypeNo
            if (typeNo == "Q") {//阅读填空
                val childListSize = paperPerAnalysisInfor.TypeList[index_A].QuesDetailList[index_B].ChildList.size
                if(childListSize == 1){
                    holder.convertView.TvChildAskPositionX.visibility = View.GONE
                    holder.convertView.TvChildAsk.visibility = View.GONE
                }else{
                    holder.convertView.TvChildAskPositionX.visibility = View.VISIBLE
                    holder.convertView.TvChildAsk.visibility = View.VISIBLE
                }

            } else if (typeNo == "X" || typeNo == "G") {//X:阅读简单 G:听力简答
                holder.convertView.TvChildAsk.visibility = View.VISIBLE
            } else if (typeNo == "k") {//阅读综合
                if (childAnswerType == 32) {//匹配题综合
                    holder.convertView.TvChildAsk.visibility = View.GONE
                } else {
                    holder.convertView.TvChildAsk.visibility = View.VISIBLE
                }
            } else if (typeNo == "b" || typeNo == "V" || typeNo == "d") {//b:补全对话 V:连词成句 d:看图写句子
                val quesChildNum = paperPerAnalysisInfor.TypeList[index_A].QuesDetailList[index_B].QuesChildNum
                if(quesChildNum == 1){
                    holder.convertView.TvChildAskPositionX.visibility = View.VISIBLE
                }else{
                    holder.convertView.TvChildAskPositionX.visibility = View.GONE
                }
                holder.convertView.TvChildAsk.visibility = View.GONE
            } else if (typeNo == "g") {//翻译题
                if (genreID == 39 || genreID == 41) {
                    //39:GenreName=篇章英译中//41:GenreName=篇章中译英
                    if (genreID == 39) {
                        childAsk = "请将上面的英文篇章翻译成中文"
                    } else if (genreID == 41) {
                        childAsk = "请将上面的中文篇章翻译成英文"
                    }
                    holder.convertView.TvChildAsk.visibility = View.VISIBLE
                }else {
                    val quesBody = paperPerAnalysisInfor.TypeList[index_A].QuesDetailList[index_B].QuesBody
                    if(quesBody.isEmpty()){
                        holder.convertView.TvChildAsk.visibility = View.GONE
                    }else{
                        holder.convertView.TvChildAsk.visibility = View.VISIBLE
                    }
                }
            }
            holder.convertView.TvChildAskPositionX.text = "$sortIndex. "
            CustomTextViewToPicture(activity, holder.convertView.TvChildAsk, childAsk)
            holder.convertView.IvChildAudioPlayA.visibility = View.GONE
        }else{
            val sortIndexType = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).SortIndexType
            var sortIndex = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ItemList[position - 1].SortIndex
            var itemAnswer = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ItemList[position - 1].ItemAnswer
            var itemAnalysis = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ItemList[position - 1].ItemAnalysis
            var stuAnswer = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[index_C].ItemList[position - 1].StuAnswer
            var imagePath = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[index_C].ItemList[position - 1].ImagePath
            var evalScore = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[index_C].ItemList[position - 1].EvalScore
            var state = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[index_C].ItemList[position - 1].State
            var scoreRate = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[index_C].ItemList[position - 1].ScoreRate

            if(imagePath.isEmpty()){
                holder.convertView.IvStuAnswerPicture.visibility = View.GONE
            }else {
                imagePath = Ext.OBSINFO?.ServerWithBucket+imagePath
                holder.convertView.IvStuAnswerPicture.visibility = View.VISIBLE
//                object : Thread() {
//                    override fun run() {
//                        super.run()
//                        try {
//                            URL(imagePath).openStream().use { inputStream ->
//                                val bitmap = BitmapFactory.decodeStream(inputStream)
//                                activity.runOnUiThread(Runnable { holder.convertView.IvStuAnswerPicture.setImageBitmap(bitmap) })
//                            }
//                        } catch (ex: IOException) {
//                            Log.e("TAG", null, ex)
//                        }
//                    }
//                }.start()
                Glide.with(activity)
                        .load(imagePath)
                        .error(R.mipmap.aibk_icon_photo_broken)
//                        .placeholder(R.drawable.aibk_drawable_dialog_loading)//加载等待图
                        .dontAnimate()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存模式
                        .into(holder.convertView.IvStuAnswerPicture);
            }

            if(stuAnswer.isEmpty()&&imagePath.isEmpty()){
                stuAnswer = "未填写"
            }
            if(sortIndexType == 0){
                holder.convertView.TvChildAskPositionY.visibility = View.GONE
            }else {
                holder.convertView.TvChildAskPositionY.visibility = View.VISIBLE
                holder.convertView.TvChildAskPositionY.text = "$sortIndex. "
            }

            if(sortIndexType == 0){
                holder.convertView.TvChildAskPositionY.visibility = View.GONE
            }else {
                holder.convertView.TvChildAskPositionY.visibility = View.VISIBLE
                holder.convertView.TvChildAskPositionY.text = "$sortIndex. "
            }

            showStuAnswerInput(holder.convertView.TvStuAnswer, stuAnswer)
            if(state == 0){
                holder.convertView.TvItemScore.text = activity.getString(R.string.aibk_state_reviewing)
                holder.convertView.TvItemScoreTitle.setTextColor(activity.resources.getColor(R.color.lancoooBlue));
                holder.convertView.TvItemScore.setTextColor(activity.resources.getColor(R.color.lancoooBlue));
            }else if(state == 1||state == 2||state == 3){
                holder.convertView.TvItemScore.text = activity.resources.getString(R.string.aibk_state_reviewed, evalScore)
                if(scoreRate>=0.6){
                    holder.convertView.TvItemScoreTitle.setTextColor(activity.resources.getColor(R.color.lancoooGreen));
                    holder.convertView.TvItemScore.setTextColor(activity.resources.getColor(R.color.lancoooGreen));
                }else{
                    holder.convertView.TvItemScoreTitle.setTextColor(activity.resources.getColor(R.color.lancoooRed));
                    holder.convertView.TvItemScore.setTextColor(activity.resources.getColor(R.color.lancoooRed));
                }
            }

            CustomTextViewToPicture(activity, holder.convertView.TvItemAnswer, LancooMethod.getNewItemAnswer(itemAnswer, false))
            CustomTextViewToPicture(activity, holder.convertView.TvItemAnalysis, LancooMethod.getNewItemAnalysis(itemAnalysis))
        }
    }

    private fun getItemTotalSize():Int{
        return getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ItemList.size
    }

    override fun getItemCount(): Int {
        return getItemTotalSize()+1
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    private fun showStuAnswerInput(tvStuAnswer: TextView, result: String){
        val linearParams = tvStuAnswer.getLayoutParams()
        if (result.isEmpty()) {
            val genreID = paperPerAnalysisInfor.TypeList[index_A].QuesDetailList[index_B].GenreID
            if(genreID == 39|| genreID == 27 || genreID == 38 || genreID == 50){
                //39:篇章英译中 27：中英互译  38：句子英译中 50:句子定向英译中
                linearParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
            }else {
                linearParams.height = 1 //高
            }
        } else {
            linearParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
        }
        tvStuAnswer.setLayoutParams(linearParams)
        tvStuAnswer.text = result
    }

}